package com.yuke.cloud.service.cmc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.RedisKeyUtil;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.cmc.dto.CmsSecKillAndSkuDto;
import com.yuke.cloud.service.api.cmc.dto.CmsSecKillDto;
import com.yuke.cloud.service.api.cmc.dto.CmsSecKillSkuDto;
import com.yuke.cloud.service.api.cmc.service.CmcFeignApi;
import com.yuke.cloud.service.api.pmc.service.PmcFeignApi;
import com.yuke.cloud.service.cmc.service.CmsSecKillService;
import com.yuke.cloud.service.cmc.service.CmsSecKillSkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/*
 * <p>Company: Copyright (c) 2018. yuke All Rights Reserved. </br> </p>
 * <p>Title：yukemall </br> </p>
 * <p>com.yuke.cloud.service.cmc.controller.frontend Description TODO </br> </p>
 * @Author wg
 * @Date   2019/6/6 15:49
 */
@Api(value = "WEB - MallCmsSecKillController", tags = "处理CmsSecKill相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/mall/cms/sec_kill")
public class MallCmsSecKillController  extends BaseController {

    @Resource
    private CmsSecKillService cmsSecKillService;

    @Resource
    private CmsSecKillSkuService cmsSecKillSkuService;
    @Autowired
    private PmcFeignApi pmcFeignApi;

    @Autowired
    private CmcFeignApi cmcFeignApi;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @GetMapping("/getSecKill")
    @ApiOperation(httpMethod = "GET", value = "获取秒杀活动及商品")
    public Wrapper<List<CmsSecKillAndSkuDto>> getSecKill(@ApiParam(name = "count", value = "需要返回的记录个数", defaultValue = "5") @RequestParam(defaultValue = "5") Integer count) {

        List<CmsSecKillAndSkuDto> cmsSecKillAndSkuDtos = cmsSecKillService.getNowSecKillAndSpecs(count);

        if (cmsSecKillAndSkuDtos == null || cmsSecKillAndSkuDtos.size() <= 0) {
            cmsSecKillAndSkuDtos = cmsSecKillService.getNextSecKillAndSpecsExt(count);
            if (cmsSecKillAndSkuDtos == null || cmsSecKillAndSkuDtos.size() <= 0) {
                cmsSecKillAndSkuDtos = cmsSecKillService.getLastSecKillAndSpecs(count);
            }
        }

        return WrapMapper.ok(cmsSecKillAndSkuDtos);
    }

    @GetMapping("/getSecKillSkus/{secKillId}")
    @ApiOperation(httpMethod = "GET", value = "获取秒杀活动的更多商品")
    public Wrapper<PageInfo<CmsSecKillSkuDto>> getSecKillSkus(@ApiParam(name = "secKillId", value = "秒杀活动id") @PathVariable Long secKillId,
                                                              @ApiParam(name = "shopId", value = "店铺ID", defaultValue = "0") @RequestParam(defaultValue = "0") Long shopId,
                                                       @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                       @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        if (secKillId != null) {
            List<CmsSecKillSkuDto> list = cmsSecKillService.getSecKillSkus(secKillId ,shopId);
            PageInfo pageInfo = new PageInfo(list);
            return WrapMapper.ok(pageInfo);
        } else {
            PageInfo pageInfo = new PageInfo(new ArrayList<>());
            return WrapMapper.ok(pageInfo);
        }
    }

//    @GetMapping("/getNowSecKill")
//    @ApiOperation(httpMethod = "GET", value = "获取正在进行的秒杀活动及商品")
//    public Wrapper<List<CmsSecKillAndSkuDto>> getNowSecKill() {
//        List<CmsSecKillAndSkuDto> cmsSecKillAndSkuDtos = cmsSecKillService.getNowSecKillAndSpecs();
//
//        return WrapMapper.ok(cmsSecKillAndSkuDtos);
//    }
//
//    @GetMapping("/getLastSecKill")
//    @ApiOperation(httpMethod = "GET", value = "获取已结束的上一个秒杀活动及商品")
//    public Wrapper<List<CmsSecKillAndSkuDto>> getLastSecKill() {
//        List<CmsSecKillAndSkuDto> cmsSecKillAndSkuDtos = cmsSecKillService.getLastSecKillAndSpecs();
//
//        return WrapMapper.ok(cmsSecKillAndSkuDtos);
//    }
//
//    @GetMapping("/getNextSecKill")
//    @ApiOperation(httpMethod = "GET", value = "获取即将开始的下一个秒杀活动及商品")
//    public Wrapper<List<CmsSecKillAndSkuDto>> getNextSecKill() {
//        List<CmsSecKillAndSkuDto> cmsSecKillAndSkuDtos = cmsSecKillService.getNextSecKillAndSpecsExt();
//
//        return WrapMapper.ok(cmsSecKillAndSkuDtos);
//    }

    @GetMapping("/loadNowSecKillToRedis")
    @ApiOperation(httpMethod = "GET", value = "将设置为正在秒杀的活动放到缓存中，用于调试！！！")
    public Wrapper loadNowSecKillAndSpecs() {

        List<CmsSecKillAndSkuDto> cmsSecKillAndSkuDtos = cmsSecKillService.getNowSecKillAndSpecs(null);

        if (cmsSecKillAndSkuDtos != null && cmsSecKillAndSkuDtos.size() > 0) {
            for (CmsSecKillAndSkuDto cmsSecKillAndSkuDto : cmsSecKillAndSkuDtos) {
                CmsSecKillDto cmsSecKillDto = cmsSecKillAndSkuDto.getCmsSecKillDto();
                Date now = new Date();
                long timeOut = cmsSecKillAndSkuDto.getCmsSecKillDto().getEndTime().getTime() - now.getTime();
                redisTemplate.opsForValue().set(RedisKeyUtil.getSecKillKey(String.valueOf(cmsSecKillDto.getSecKillId())),
                        cmsSecKillDto, timeOut, TimeUnit.MILLISECONDS);

                List<CmsSecKillSkuDto> cmsSecKillSkuDtos = cmsSecKillAndSkuDto.getCmsSecKillSkuDtos();
                if (cmsSecKillSkuDtos != null && cmsSecKillSkuDtos.size() > 0) {
                    for (CmsSecKillSkuDto cmsSecKillSkuDto : cmsSecKillSkuDtos) {
                        redisTemplate.opsForValue().set(RedisKeyUtil.getSecKillSkuKey(String.valueOf(cmsSecKillSkuDto.getSecKillId()),
                                String.valueOf(cmsSecKillSkuDto.getSecKillSkuId())), cmsSecKillSkuDto, timeOut, TimeUnit.MILLISECONDS);
                    }
                }
            }
        }

        return WrapMapper.ok();
    }


}
